//
//  SNOrderFormDetailModel.m
//  app
//
//  Created by 王少宁 on 15/5/6.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//  订单性情的基本 model

#import "SNOrderFormDetailModel.h"

@implementation SNOrderFormDetailModel

-(instancetype)initwithDict:(NSDictionary *)dict
{
    if ([super init]) {
        _sn = dict[@"sn"];
        _money = [NSString stringWithFormat:@"%.2f",[dict[@"money"] floatValue]];
        _ostatus = [dict[@"ostatus"] stringValue];
        _createTime = [XCommon dateWithDateline:dict[@"createTime"]];
        _isReturns = [dict[@"isReturns"] stringValue];
        DLog(@"%@",_createTime);
        
        //收货人信息
        _receiverInfoModel = [SNOrderFormReceiverInfoModel modelWithDict:dict[@"receiver_info"] expressInfoDic:dict[@"express_info"] storehouse:dict[@"storehouse"]];
        
        //配送时间,发票信息,买家留言
        NSMutableDictionary * basicDict = [NSMutableDictionary dictionary];
        [basicDict setObject:dict[@"express_info"] forKey:@"express_info"];//快递信息
        [basicDict setObject:dict[@"invoice_info"] forKey:@"invoice_info"];//发票信息
        [basicDict setObject:dict[@"notes"] forKey:@"notes"];//留言
        _basicModel = [SNOrderFormDetailBasicModel modelWithDict:basicDict];
        
        //商品列表
        NSMutableArray * mutablArray = [NSMutableArray array];
        
        NSArray * product_infoArray = dict[@"product_info"];
        for (NSDictionary * dic in product_infoArray) {
            
            BOOL commentBtn = NO;
            
            if (!_isReturns.boolValue &&[_ostatus isEqualToString:@"4"]) {
                commentBtn = YES;
            }
            
            ReturnGoodsBtnType returnGoodsBtnType = ReturnGoodsNone;
            
            //判断是否含有 申请售后 按钮的类型
            NSArray * returnDisplayStauts = @[@"1",@"8",@"9",@"2"];
            if (!_isReturns.boolValue && [returnDisplayStauts containsObject:_ostatus]) {
                returnGoodsBtnType = ReturnGoodsReturnApply;
            }
            
            SNOrderFormDetailGoodsModel * model = [SNOrderFormDetailGoodsModel modelWithDict:dic commentBtn:commentBtn returnGoodsBtnType:returnGoodsBtnType];
            
            [mutablArray addObject:model];
        }
        _productArray = mutablArray;
        
        //商品金额
        NSMutableDictionary * footerDict = [NSMutableDictionary dictionary];
        //合计支付 (也就是订单金额)
        [footerDict setObject:dict[@"money"] forKey:@"lastmoney"];
        //运费
        [footerDict setObject:dict[@"express_info"][@"express_fee"] forKey:@"express_fee"];
        //金豆相关字典
        [footerDict setObject:dict[@"gold"] forKey:@"gold"];
        //金额总计 (也就是原价)
        float originallyMoney = 0;
        for (NSDictionary * dic in product_infoArray) {
            originallyMoney += [dic[@"price"] floatValue] * [dic[@"product_num"] integerValue];
        }
        [footerDict setObject:[NSString stringWithFormat:@"%.2f",originallyMoney] forKey:@"originallymoney"];
        
        _footerModel = [SNOrderFormListFooterModel modelWithDict:footerDict];
        
        //客服信息
        NSArray * customerServiceArrays = dict[@"CustomerServices"];
        _custonmerServiceModel = [SNCustomerServiceModel modelWithDict:customerServiceArrays.firstObject];
    }
    return self;
}

+(instancetype)modelWithDict:(NSDictionary *)dict
{
    return [[self alloc] initwithDict:dict];
}

@end
